
package com.eworking.eim.user;

import com.eworking.eim.database.EIMDatabaseManager;
import com.eworking.eim.database.EIMDatabaseTable;
import com.eworking.eim.util.EIMLog;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class EIMUserTable implements EIMDatabaseTable {
    
    /** 日志输出标记 */
    private static final String _TAG = EIMUserTable.class.getSimpleName();
    
    /** 表名 */
    private static final String _TABLE_NAME = "IM_USER";
    
    private static EIMUserTable userTable = null;
    
    /** 数据库管理 */
    private EIMDatabaseManager databaseManager = null;
    
    public EIMUserTable(EIMDatabaseManager databaseManager) {
        this.databaseManager = databaseManager;
    }
    
    public static EIMUserTable getInstance() {
        return userTable;
    }
    
    /**
     * 添加好友用户
     * 
     * @param JID
     * @param account
     */
    public void addUser(String JID, String account) {
        try {
            SQLiteDatabase database = this.databaseManager.getWritableDatabase();
            ContentValues values = new ContentValues();
            values.put(Fields._JID, JID);
            values.put(Fields._ACCOUNT, account);
            database.insert(_TABLE_NAME, null, values);
        } catch (Exception e) {
            EIMLog.e(_TAG, "添加好友用户失败:" + JID);
        }
    }
    
    /**
     * 删除好友用户
     * 
     * @param JID
     */
    public void deleteUser(String JID) {
        try {
            SQLiteDatabase database = this.databaseManager.getWritableDatabase();
            database.delete(_TABLE_NAME, Fields._JID + " = ? ", new String[] { JID });
        } catch (Exception e) {
            EIMLog.e(_TAG, "删除好友用户失败:" + JID);
        }
    }
    
    public void loadUsers() {
        // 查询语句
        String sql = "SELECT " + Fields._JID + " , " + Fields._ACCOUNT + " FROM " + _TABLE_NAME;
        try {
            SQLiteDatabase database = this.databaseManager.getWritableDatabase();
            Cursor cursor = database.rawQuery(sql, null);
            if (!(cursor.moveToFirst())) {
                cursor.close();
                return;
            }
            do {
                EIMUser user = new EIMUser(cursor.getString(0), cursor.getString(1));
                EIMLog.d(_TAG, "加载用户信息：" + user);
                // 放入缓存
                EIMUserManager.getInstance().addUserFormDB(user);
            } while (cursor.moveToNext());
            cursor.close();
        } catch (Exception e) {
            EIMLog.e(_TAG, "查询用户信息失败：" + e.getMessage());
        }
    }
    
    @Override
    public void onCreated(SQLiteDatabase database) {
        String sql =
            "CREATE TABLE " + _TABLE_NAME + " (" + Fields._JID + " TEXT PRIMARY KEY ," + Fields._ACCOUNT
                + " TEXT NOT NULL);";
        EIMLog.d(_TAG, "创建表：" + _TABLE_NAME + " : " + sql);
        database.execSQL(sql);
    }
    
    @Override
    public void onDroped(SQLiteDatabase database) {
        // 删除表语句
        String sql = "DROP TABLE IF EXISTS " + _TABLE_NAME;
        database.execSQL(sql);
    }
    
    private static final class Fields {
        
        /** 用户JID = 企业账号 */
        private static final String _JID = "jid";
        
        /** 用户账号 */
        private static final String _ACCOUNT = "account";
    }
}
